package com.threeox.httplibrary.builder;

import com.threeox.httplibrary.request.RequestCall;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @ClassName: OkHttpRequestBuilder
 *
 * @Description: Todo(请求方式的抽象类)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2016/12/21 11:06
 *
 * @version 1.0
 */
public abstract class OkHttpRequestBuilder<T extends OkHttpRequestBuilder> {

    protected String url;
    protected Object tag;
    protected Map<String, Object> headers;
    protected Map<String, Object> params;
    protected int id;

    public T id(int id) {
        this.id = id;
        return (T) this;
    }

    public T url(String url) {
        this.url = url;
        return (T) this;
    }


    public T tag(Object tag) {
        this.tag = tag;
        return (T) this;
    }

    /**
     * 添加头部请求参数
     *
     * @param headers
     * @return
     */
    public T headers(Map<String, Object> headers) {
        if (null == this.headers){
            this.headers = new HashMap<String, Object>();
        }
        if (null != headers && !headers.isEmpty()) {
            this.headers.putAll(headers);
        }
        return (T) this;
    }

    /**
     * 添加头部请求参数
     *
     * @param key
     * @param val
     * @return
     */
    public T addHeader(String key, String val) {
        if (null == this.headers){
            headers = new HashMap<String, Object>();
        }
        headers.put(key, val);
        return (T) this;
    }

    /**
     * 清除所有头部请求参数
     *
     * @return
     */
    public T clearHeader() {
        if (this.headers != null && this.headers.isEmpty()) {
            this.headers.clear();
        }
        return (T) this;
    }

    /**
     * 添加请求参数
     *
     * @param params
     * @return
     */
    public T params(Map<String, Object> params) {
        if (null == this.params){
            this.params = new HashMap<String, Object>();
        }
        if (null != params && !params.isEmpty()) {
            this.params.putAll(params);
        }
        return (T) this;
    }

    /**
     * 添加请求参数
     *
     * @param key
     * @param val
     * @return
     */
    public T addParams(String key, Object val) {
        if (null == this.params) {
            this.params = new HashMap<String, Object>();
        }
        this.params.put(key, val);
        return (T) this;
    }

    /**
     * 得到所有的请求参数
     *
     * @return
     */
    public Map<String, Object> getParams() {
        return params;
    }

    /**
     * 清除所有请求参数
     *
     * @return
     */
    public T clearParams() {
        if (this.params != null && !this.params.isEmpty()) {
            this.params.clear();
        }
        return (T) this;
    }

    public abstract RequestCall build();
}
